package test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class NextBirthday {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("生日(月份-天):");
        String bir = in.next();
        Calendar d = Calendar.getInstance();
        int year = d.get(Calendar.YEAR);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        df.setLenient(false);
        String birthday = year + "-" + bir;//yyyy-mm-dd

        boolean geshi = false;
        Date date = null;
        Calendar now = Calendar.getInstance();
        //清除时分秒毫秒
        now.clear(Calendar.HOUR_OF_DAY);
        now.clear(Calendar.HOUR);
        now.clear(Calendar.MINUTE);
        now.clear(Calendar.SECOND);
        now.clear(Calendar.MILLISECOND);
        for (int i = 0; i < 8; i++) {
            System.out.println(birthday);
            try {
                date = df.parse(birthday);
                d.setTime(date);//
                geshi = true;
                System.out.println(now.getTime());
                System.out.println(d.getTime());
                if (now.compareTo(d) <= 0) {//生日还没有到,找到下次生日的时间
                    break;//date时间真的你下次生日的时间
                }
                year++;
                birthday = year + "-" + bir;
            } catch (ParseException e) {
                year++;
                birthday = year + "-" + bir;
            }
        }
        if (!geshi) {
            System.out.println("格式输入错误");
            return;
        }
        System.out.println("格式正确");
        System.out.println(date);
        long days = (date.getTime() - now.getTimeInMillis()) / 1000 / (60 * 60 * 24);
        System.out.printf("距您下次生日还有 %d天\n", days);
    }
}
